package com.bj58.wuxian.lucene.ikanalyzer;

import java.io.IOException;
import java.nio.file.Paths;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

import com.bj58.wuxian.lucene.constants.Constants;
import com.bj58.wuxian.lucene.utils.LuceneUtil;

public class IkAnaklyzerDemo {

	public static void indexByAnalyzer(Analyzer analyzer) {

		Directory directory = null;
		IndexWriter indexWriter = null;
		try {
			directory = LuceneUtil.getDirectory("D:\\index");
			indexWriter = LuceneUtil.getIndexWriter(directory, analyzer);

			Document document = new Document();
			document.add(new StringField("title", "三国演义", Store.YES));
			document.add(new StringField("author", "罗贯中", Store.YES));
			document.add(new TextField("desc", "《三国演义》是中国古典四大名著之一", Store.YES));
			indexWriter.addDocument(document);
			System.out.println("**********index success!!!**********");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			LuceneUtil.close(indexWriter, directory);
		}
	}

	public static void searchByAnalyzer(String field, String value, Analyzer analyzer) {
		Directory directory = null;
		DirectoryReader reader = null;
		try {
			directory = LuceneUtil.getDirectory(Constants.DIRECTROY_PATH);
			reader = LuceneUtil.getDirectoryReader(directory);
			IndexSearcher indexSearcher = LuceneUtil.getIndexSearcher(reader);
			QueryParser parser = new QueryParser(field, analyzer);
			Query query = parser.parse(value);
			ScoreDoc[] docs = indexSearcher.search(query, 10).scoreDocs;
			for (int i = 0; i < docs.length; i++) {
				int docId = docs[i].doc;
				System.out.println(docs[i].doc);
				Document document = indexSearcher.doc(docId);
				System.out.println("title:" + document.get("title") + "   author:" + document.get("desc") + "   desc:"
						+ document.get("author"));
			}

		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			LuceneUtil.close(reader, directory);

		}
	}

	public static void searchByTerm(String field, String value) {
		Directory directory = null;
		DirectoryReader reader = null;
		try {
			directory = LuceneUtil.getDirectory(Constants.DIRECTROY_PATH);
			reader = LuceneUtil.getDirectoryReader(directory);
			IndexSearcher indexSearcher = LuceneUtil.getIndexSearcher(reader);
			Term term = new Term(field, value);
			TermQuery query = new TermQuery(term);
			ScoreDoc[] docs = indexSearcher.search(query, 10).scoreDocs;
			for (int i = 0; i < docs.length; i++) {
				int docId = docs[i].doc;
				System.out.println(docs[i].doc);
				Document document = indexSearcher.doc(docId);
				System.out.println(document.get(field));
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			LuceneUtil.close(reader, directory);
		}

	}

	// 根据StandardAnalyzer 创建索引
	@Test
	public void indexByStandardAnalyzer() {
		Analyzer analyzer = new StandardAnalyzer();
		indexByAnalyzer(analyzer);

	}

	// 根据StandardAnalyzer 进行搜索
	@Test
	public void searchByStandardAnalyzer() {
		Analyzer analyzer = new StandardAnalyzer();
		searchByAnalyzer("desc", "三国演义", analyzer);

	}

	@Test
	public void searchByTermQuery() {
		searchByTerm("title", "三国演义");

	}

	// 根据IKAnalyzer 创建索引
	@Test
	public void indexByIKAnalyzer() {
		Analyzer analyzer = new IKAnalyzer();
		indexByAnalyzer(analyzer);

	}

	// 根据IKAnalyzer 进行搜索
	@Test
	public void searchByIKAnalyzer() {
		Analyzer analyzer = new IKAnalyzer();
		searchByAnalyzer("desc", "三国演义", analyzer);

	}

}
